<?php
// 应用公共文件
/**
 * curl get 请求
 * @param $url
 * @return mixed
 */
function httpGet($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);
    $res = curl_exec($curl);
    curl_close($curl);
    return $res;
}

/**
 * curl post 请求
 * @param $post_data
 * @param $url
 * @return mixed
 */
function httpPost($url,$post_data) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
    $res = curl_exec($curl);
    curl_close($curl);
    return $res;
}

/**
 * curl put 请求
 * @param $Url
 * @param $data
 * @return mixed
 */
function put($url,$data){
    // 创建一个新cURL资源
    $ch = curl_init();
    // 设置URL和相应的选项
    curl_setopt($ch, CURLOPT_URL, $url); //$url设置需要请求的url
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //设置请求头
    $headerArray = ["Content-type:application/json"];
    curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray); 
    //设置请求方式和提交的字符串
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"PUT"); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    // 抓取URL并把它传递给浏览器
    $output = curl_exec($ch);
    // 关闭cURL资源，并且释放系统资源
    curl_close($ch);
    //print(json_decode($output,true))
}

/**
 * curl delete 请求
 * @param $Url
 * @param $data
 * @return mixed
 */
function delete($url,$data){// 创建一个新cURL资源
    $ch = curl_init();
    // 设置URL和相应的选项
    curl_setopt($ch, CURLOPT_URL, $url); //$url设置需要请求的url
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //设置请求头
    $headerArray = ["Content-type:application/json"];
    curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray); 
    //设置请求方式和提交的字符串
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"DELETE"); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    // 抓取URL并把它传递给浏览器
    $output = curl_exec($ch);
    // 关闭cURL资源，并且释放系统资源
    curl_close($ch);
    //print(json_decode($output,true))
}
/**
 * curl patch 请求
 * @param $Url
 * @param $data
 * @return mixed
 */
function patch($url,$data){
    // 创建一个新cURL资源
    $ch = curl_init();
    // 设置URL和相应的选项
    curl_setopt($ch, CURLOPT_URL, $url); //$url设置需要请求的url
    //参数设置为 1 是把curl_exec()的结果转为字符串
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //设置请求头
    $headerArray = ["Content-type:application/json"];
    curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray); 
    //设置请求方式和提交的字符串
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"PATCH"); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    // 抓取URL并把它传递给浏览器
    $output = curl_exec($ch);
    // 关闭cURL资源，并且释放系统资源
    curl_close($ch);
    //print(json_decode($output,true))
}


/**
 * 随机生成数字和字母
 * @param  [type] $length   字符串长度
 * @param  [type] $flag   str:生成字符串，num:生成数字
 */
function getRandStr($length,$flag='str') {
    if($flag=='str'){
        $str     = 'abcdefghijklmnopqrstuvwxyz0123456789';
    }else{
        $str     = '0123456789';
    }
    $len     = strlen($str) - 1;
    $randstr = '';
    for ($i = 0; $i < $length; $i++) {
        $num = mt_rand(0, $len);
        $randstr .= $str[$num];
    }
    return $randstr;
}

/**
 * 字符串截取
 * @param  [type] $str      字符串
 * @param  [type] $position 位置
 * @return [type]           [description]
 */
function sub_str($str,$position){
    $newStr=substr($str,0,strpos($str,$position));
    return $newStr;
}

/**
 * 加密解密方法
 * @param  [type] $str  加密字符串
 * @param  [type] $type 状态1：加密，0:解密
 * @return [type]          [description]
 */
function encryptDecrypt($str,$type=1){
    //密钥
    $key=config('base.token_secret_key');
    if($type){
        //加密
        $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $str, MCRYPT_MODE_CBC, md5(md5($key))));   
    }else{
        //解密
        $decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($str), MCRYPT_MODE_CBC, md5(md5($key))), "12");   
    }
    return $decrypted;
}

//生成key
function createKey(){
    return 'h'.time();
}

/**
 * 判断变量是否存在
 * @param  [type]  $value       [description]
 * @param  string  $defaultValue 默认值
 * @return boolean               [description]
 */
function isInput($value,$defaultValue=''){
    if(isset($value) && !empty($value)){
        return $value;
    }
    return $defaultValue;
}


//=============== start 参数加密，解密 =================//
//加密
function passport_encrypt($arr) {
    $txt=implode($arr,'&');
    //检查加密字符中是否有 "/"
    $encryptStr=passport_check_slash($txt);
    return urlencode($encryptStr);
}  
//解密
function passport_decrypt($txt) {
    $key=config('config.params_secret_key');
    $txt = passport_key(base64_decode(urldecode($txt)), $key);
    $tmp = '';
    for($i = 0;$i < strlen($txt); $i++) {
        $md5 = $txt[$i];
        $tmp .= $txt[++$i] ^ $md5;
    }
    $tmpArr=explode('&',$tmp);
    return $tmpArr;
}
//解析算法
function passport_key($txt, $encrypt_key) {
    $encrypt_key = md5($encrypt_key);
    $ctr = 0;
    $tmp = '';
    for($i = 0; $i < strlen($txt); $i++) {
        $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
        $tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
    }
    return $tmp;
}
//检查加密字符中是否有 "/"
function passport_check_slash($txt){
    $key=config('config.params_secret_key');
    srand((double)microtime() * 1000000);
    $encrypt_key = md5(rand(0, 32000));
    $ctr = 0;
    $tmp = '';
    for($i = 0;$i < strlen($txt); $i++) {
        $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
        $tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]);
    }
    $encryptStr=base64_encode(passport_key($tmp, $key));
    if (strpos($encryptStr, "/") !== false){
        //斜杠"/"存在
        $encryptStr=passport_check_slash($encryptStr);
    }
    return $encryptStr;
}
//=============== end 参数加密，解密 =================//

//=============== start 参数加密，解密 =================//
//参数加密
function paramEncrypt($data){
    $key = config('config.params_secret_key'); // 密钥应足够长且随机
    $ivlen = openssl_cipher_iv_length($cipher="AES-128-CBC");
    $iv = openssl_random_pseudo_bytes($ivlen);
    $ciphertext = openssl_encrypt($data, $cipher, $key, $options=0, $iv);

    // 将 IV 添加到加密文本中以便后续解密时使用
    return base64_encode($iv . $ciphertext);
}

//参数解密
function paramDecrypt($string){
    $combined = base64_decode($string);
    $ivlen = openssl_cipher_iv_length($cipher="AES-128-CBC");
    $iv = substr($combined, 0, $ivlen);
    $ciphertext = substr($combined, $ivlen);
    $key = config('config.params_secret_key');
    return openssl_decrypt($ciphertext, $cipher, $key, $options=0, $iv);
}
//=============== end 参数加密，解密 =================//

//验证是不是base64
function isBase64($str) {
    $str = str_replace(' ', '', $str);
    return base64_encode(base64_decode($str)) === $str;
}
